/**
 * @author fenglei.you
 */
Me.TiledLayer=function(image,unitW,unitH,x,y,width,height){
	this.image=image;
	this.unitW=unitW;
	this.unitH=unitH;
	
	this.tiles=[];
	
	this.x=x||0;
	this.y=y||0;
	this.width=width||0;
	this.height=height||0;
	
	
	var img = new Image();	
	img.src = this.image;		
	this.img = img;	
}
Me.TiledLayer.prototype.isFullScreen=function(){
	return this.x+this.y+this.width+this.height==0;
}
Me.TiledLayer.prototype.setData=function(data,render,callback){	
	this.data=data;
	if(this.data && this.data.length>0){
		for(var idx in this.data){
			var elem = this.data[idx];
			
			var imageObj={};
			imageObj.img=this.img;
			imageObj.x=elem.imgX;
			imageObj.y=elem.imgY;
			imageObj.width=elem.imgWidth;
			imageObj.height=elem.imgHeight;
			
			var tile = new Me.Tile(elem.x,elem.y,elem.width,elem.height,imageObj);
			this.tiles[idx] = tile;			
		}	
	}else{
		Trace.log("no data setted");
		return ;
	}
	
	if(render){
		this._render(callback);
	}
}
Me.TiledLayer.prototype._render=function(context,callback){
	Trace.log("tiledlayer start reder");
	
	if(this.tiles && this.tiles.length>0){
		var len = this.tiles.length;
		
		var buffer = Me.DBF.getCanvas();
		var bContext = buffer.getContext('2d');
		for(var index =0;index<len;index++){
			var cuTile = this.tiles[index];
			cuTile.paint(bContext);
		}
		
		context.drawImage(buffer,0,0);
	}
	
	Trace.log("tiledlayer finish reder");
	if(callback)callback();
}
Me.TiledLayer.prototype.render=function(context,callback){
	this._render(context,callback);	
}


